package ${packageName}.service.impl;

import org.springframework.stereotype.Service;
import ${packageName}.db.mapper.${mapperName};
import ${packageName}.service.${serviceName};
import cn.hutool.core.collection.CollUtil;
import com.eco.common.commonbase.mybatis.PageParam;
import com.eco.common.commonbase.mybatis.BaseServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.common.commonbase.exception.ServiceException;
import com.eco.scrm.atmosphere.framework.context.SpringContext;
import ${packageName}.db.entity.${entityName};
import ${packageName}.pojo.req.${addReqDto};
import ${packageName}.pojo.req.${updateReqDto};
import ${packageName}.pojo.req.${queryPageReqDto};
import ${packageName}.pojo.resp.${queryResDto};
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;

import java.util.List;

@Slf4j
@Service
public class ${serviceImplName} extends BaseServiceImpl<${mapperName}, ${entityName}> implements ${serviceName} {

    /**
     * 分页查询
     */
    @Override
    public IPage<${queryResDto}> query${entityName}Page(PageParam<${queryPageReqDto}> pageParam) {
        int uid = SpringContext.getUserInfo().getId();
        ${queryPageReqDto} req = pageParam.getEntity();
        Page<${entityName}> page = this.page(PageParam.convertPageParam(pageParam), Wrappers.lambdaQuery(${entityName}.class)
            .eq(${entityName}::getUserId,uid));
        return page.convert(${entityNameLower} -> {
            ${queryResDto} queryResDto = new ${queryResDto}();
            BeanUtils.copyProperties(${entityNameLower}, queryResDto);
            return queryResDto;
        });
    }

    /**
     * 添加
     */
    @Override
    public Boolean add${entityName}(${addReqDto} reqDto) {
        ${entityName} ${entityNameLower} = new ${entityName}();
        int uid = SpringContext.getUserInfo().getId();
        BeanUtils.copyProperties(reqDto, ${entityNameLower});
        ${entityNameLower}.setUserId(uid);
        ${entityNameLower}.setCreateBy(uid);
        return this.save(${entityNameLower});
    }

    /**
     * 编辑
     */
    @Override
    public Boolean update${entityName}(${updateReqDto} reqDto) {
        int uid = SpringContext.getUserInfo().getId();
        ${entityName} ${entityNameLower} = this.getOne(Wrappers.lambdaQuery(${entityName}.class)
            .eq(${entityName}::getId, reqDto.getId())
            .eq(${entityName}::getUserId, uid));
        if (${entityNameLower} == null) {
            throw new ServiceException("编辑信息不存在");
        }
        BeanUtils.copyProperties(reqDto, ${entityNameLower});
        return this.updateById(${entityNameLower});
    }

    /**
     * 批量删除
     */
    @Override
    public Boolean deleteBatch(List<Long> ids){
        int uid = SpringContext.getUserInfo().getId();
        if(CollUtil.isEmpty(ids)){
            throw new ServiceException("请选择要删除的数据");
        }
        return this.remove(Wrappers.lambdaUpdate(${entityName}.class)
            .in(${entityName}::getId, ids)
            .eq(${entityName}::getUserId, uid));
    }
}